import random
import time
from bs4 import BeautifulSoup
import requests

class Download():
	def __init__(self):
		self.iplist=[]
		headers={'referer': 'http://www.xicidaili.com/nn/1', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0'}
		html=requests.get('http://www.xicidaili.com/nn/1',headers=headers)
		ips=BeautifulSoup(html.text,'lxml').find_all('tr')
		for x in range(1,len(ips)):
			ip=ips[x]
			tds=ip.find_all('td')
			i=tds[1].text+':'+tds[2].text
			self.iplist.append(i)
		self.user_agent_list=[
			"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1",
            "Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1092.0 Safari/536.6",
            "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1090.0 Safari/536.6",
            "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/19.77.34.5 Safari/537.1",
            "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.9 Safari/536.5",
            "Mozilla/5.0 (Windows NT 6.0) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.36 Safari/536.5",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",
            "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_0) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",
            "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3",
            "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",
            "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",
            "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.0 Safari/536.3",
            "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24",
            "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24"
        ]
	def get(self,url,timeout,proxy=None,num_retries=6):
		print(u'开始获取',url)
		UA=random.choice(self.user_agent_list)
		headers={'User-Agent':UA}
		return requests.get(url,headers=headers,timeout=timeout)
	def getimg(self,url,Referer,timeout,proxy=None,num_retries=6):
		print(u'开始获取',url)
		UA=random.choice(self.user_agent_list)
		headers={'User-Agent':UA,'Referer':Referer}
		if proxy==None:
			try:
				return requests.get(url,headers=headers,timeout=timeout)
			except:
				if num_retries>0:
					time.sleep(10)
					print(u'获取网页出错，10s后将获取倒数第：',num_retries,u'次')
					return self.getimg(url,timeout,num_retries-1)
				else:
					print(u'开始使用代理')
					time.sleep(10)
					IP=''.join(str(random.choice(self.iplist)).strip())
					proxy={'http':IP}
					return self.getimg(url,timeout,proxy,)
		else:
			try:
				return requests.get(url,headers=headers,proxies=proxy,timeout=timeout)
			except:
				if num_retries>0:
					time.sleep(10)
					IP=''.join(str(random.choice(self.iplist)).strip())
					proxy={'http':IP}
					print(u'正在更换代理，10s后将重新获取倒数第',num_retries,u'次')
					print(u'当前代理是：',proxy)
					return self.getimg(url.timeout,proxy,num_retries-1)
				else:
					print(u'代理也不好使，取消代理')
					return self.getimg(url,3)
request=Download()